#!/usr/bin/env python
import time
import struct
import os
#import network
import socket


DEVICE = "DC400-0001"
GONGYI = "FCAW-S"
HANGONG = "JSYJ-001"
GONGWEI = "GDGW-T01-ZET02-JS01"
IP="101.201.239.239"
PORT=502

class ModBus:
    def __init__(self, slave=0x01):
        self.tid = 0x0001
        self.protocol = 0x0000
        self.len = 0x0069
        self.slave = slave      #uint_8
        self.function = 0x10    #uint_8
        self.reg_addr = 0x0000;
        self.reg_num  = 0x0031;
        self.reg_len  = 0x62;   #uint_8
        self.time =    bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) #12 bytes
        self.device =  bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])        #10 bytes
        self.gongyi  = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])       #10 bytes
        self.hangong = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])        #10 bytes
        self.gongwei = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])       #20 bytes
        self.current = 0.0
        self.voltage = 0.0
        self.sssd = 0.0
        self.airflu = 0.0
        #to be modified
        self.speed = 0.0
        self.hqbf = 0.0
        self.hqbs = 0.0
        self.hjcd = 0.0
        self.hjjd = 0.0

    def set_device(self, device=DEVICE, gongyi=GONGYI, hangong=HANGONG, gongwei=GONGWEI):
        l = len(device)
        i = 0
        while i < l - 1:
            self.device[i] = ord(device[i+1])
            self.device[i+1] = ord(device[i])
            i += 2 

        l = len(gongyi)
        i = 0
        while i < l - 1:
            self.gongyi[i] = ord(gongyi[i+1])
            self.gongyi[i+1] = ord(gongyi[i])
            i += 2 


        l = len(hangong)
        i = 0
        while i < l - 1:
            self.hangong[i] = ord(hangong[i+1])
            self.hangong[i+1] = ord(hangong[i])
            i += 2 

        l = len(gongwei)
        i = 0
        while i < l - 1:
            self.gongwei[i] = ord(gongwei[i+1])
            self.gongwei[i+1] = ord(gongwei[i])
            i += 2 

    def set_current(self, current, voltage, sssd, airflu):
        self.current = current
        self.voltage = voltage
        self.sssd = sssd
        self.airflu = airflu

    def __set_time(self):
        now = time.localtime()
        nowstr = "{:0>2d}{:0>2d}{:0>2d}{:0>2d}{:0>2d}{:0>2d}".format((now.tm_year - 2000), 
         now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
        print (nowstr)
        for x in range(6):
            self.time[2*x] = ord(nowstr[2*x+1])
            self.time[2*x + 1] = ord(nowstr[2*x])

    def gen_bytes(self):
        offset = 0
        out = bytearray(111)

        self.__set_time()

        struct.pack_into(">H", out, offset, self.tid)
        offset += 2

        struct.pack_into(">H", out, offset, self.protocol)
        offset += 2

        struct.pack_into(">H", out, offset, self.len)
        offset += 2

        struct.pack_into(">B", out, offset, self.slave)
        offset += 1

        struct.pack_into(">B", out, offset, self.function)
        offset += 1

        struct.pack_into(">H", out, offset, self.reg_addr)
        offset += 2

        struct.pack_into(">H", out, offset, self.reg_num)
        offset += 2

        struct.pack_into(">B", out, offset, self.reg_len)
        offset += 1
        ##########
        
        out[offset: offset+12] = self.time
        offset += 12
        
        out[offset: offset+10]  = self.device 
        offset += 10

        out[offset: offset+10] = self.gongyi
        offset += 10

        out[offset: offset+10] = self.hangong
        offset += 10

        out[offset: offset+20] = self.gongwei
        offset += 20

        currentbytes = struct.pack("<f", self.current)
        out[offset: offset+2] = currentbytes[0:2]
        out[offset+2: offset+3] = currentbytes[3:4]  #shit 
        out[offset+3: offset+4] = currentbytes[2:3]  #shit
        offset += 4
       
        voltagebytes = struct.pack("<f", self.voltage)
        out[offset: offset+2] = voltagebytes[0:2]
        out[offset+2: offset+3] = voltagebytes[3:4]
        out[offset+3: offset+4] = voltagebytes[2:3]
        offset += 4

        sssdbytes = struct.pack("<f", self.sssd)
        out[offset: offset+2] = sssdbytes[0:2]
        out[offset+2: offset+3] = sssdbytes[3:4]
        out[offset+3: offset+4] = sssdbytes[2:3]
        offset += 4

        airflubytes = struct.pack("<f", self.airflu)
        out[offset: offset+2] = airflubytes[0:2]
        out[offset+2: offset+3] = airflubytes[3:4]
        out[offset+3: offset+4] = airflubytes[2:3]
        offset += 4
        ####to be realized
        
        self.tid += 1
        return out

if __name__ == '__main__':
    modbus =  ModBus()
    modbus.set_device(DEVICE, GONGYI, HANGONG, GONGWEI)

    skt = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    skt.connect((IP, PORT))

    while True:
        modbus.set_current(220.0, time.time()%10, 1, 1)
        out =  modbus.gen_bytes()
        skt.send(out) 
        time.sleep(1)

